/*************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/


/**
 * @file
 * Session.h
 *
 * @author
 * Pavel Hasenohrl (<a href="mailto:hasenpav@fel.cvut.cz">hasenpav@fel.cvut.cz</a>)
 *
 * @brief
 * Class CSession controls communication between rtmp and sip/rtp.
 */


#ifndef _SESSION_H
#define _SESSION_H

extern "C" {
#include <pthread.h>
}
#include "Rtmp.h"
#include "Rtp.h"
#include "sip/SIPClient2.h"

/**
 * @brief
 * Enumeration type identifies session states.
 */
enum SessionState {
  SS_STARTED,
  SS_STOPED,
  SS_CONNECTED,
  SS_DIALING,
  SS_RTMP_HANG_OUT,
  SS_SIP_HANG_OUT,
  SS_TALKING,
  SS_ERROR
};

class CPortManager;

    /**
     * @brief
     * Class controls comunication between rtmp and sip/rtp.
     */
class CSession {



     public:
          /**
           * @brief Constructor.
           * @param[in] iSocket opened TCP socket, which communicates with RTMP client
           * @param[in] conf initialized instance of CConfig
           */
          CSession(int iSocket, CPortManager &pman);
          ~CSession();

          /**
           * @brief Initializes instance.
           * @return true, if initialization has been completed successfully
           */
          bool Init();

          /**
           * @brief Creates and runs a thread.
           * @return true if thread has been created successfully
           */
          bool Start();

          /**
           * @brief Implements session main logic.
           */
          void Run();

    private:
      void Handshake();
      void HandleEvent();
      void DialCall();
      void RtmpHangOut();
      void SipHangOut();
      void ProcessCall();
      void SetState(SessionState state);

    private:
      SessionState m_state;
      int m_iMinorState;
      pthread_t m_thread;
      CPortManager &m_pman;
      CRtmp m_rtmp;
      CRtp  m_rtp;
      CSipClient2 m_sip;
      int m_iSocket;    // pouziti napr. jako is pro RTP session, protoze socket je unikatni

};

#endif

